﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Text;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;
using System.Collections.ObjectModel;

namespace BrainTechLLC
{
    public static class OutputExtensions
    {
        /// <summary>
        /// If s does not end with a backslash, adds it on, otherwise returns the original string
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string AddBackslashIfNecessary(this string s)
        {
            if (!s.EndsWith("\\"))
                return s + "\\";

            return s;
        }

        /// <summary>
        /// Builds a list from the list items, using comma as the delimiter and function del to format each item
        /// Does not include null list items
        /// This version takes a StringBuilder parameter so that a new one doesn't have to be created
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="sbReused"></param>
        /// <param name="del"></param>
        /// <returns></returns>
        public static string BuildList<T>(this List<T> items, StringBuilder sbReused, Func<T, string> del)
        {
            return items.BuildList(sbReused, ", ", del);
        }

        /// Builds a list from the enumerable items, using comma as the delimiter and function del to format each item
        /// Does not include null list items
        public static string BuildList<T>(this List<T> items, Func<T, string> del)
        {
            if (items == null)
            {
                return string.Empty;
            }

            StringBuilder sb = new StringBuilder();
            return items.BuildList(sb, del);
        }

        /// <summary>
        /// Builds a list from the enumerable items, using comma as the delimiter and function del to format each item
        /// Does not include null list items
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="del"></param>
        /// <returns></returns>
        public static string BuildList<T>(this IEnumerable<T> items, Func<T, string> del) where T : class
        {
            return items.BuildList(", ", del);
        }

        /// <summary>
        /// Builds a list from the enumerable items, using parameter 'delimiter' as the delimiter and function del to format each item
        /// Does not include null list items
        /// This version takes a StringBuilder parameter so that a new one doesn't have to be created
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="sbReused"></param>
        /// <param name="delimiter"></param>
        /// <param name="del"></param>
        /// <returns></returns>
        public static string BuildList<T>(this List<T> items, StringBuilder sbReused, string delimiter, Func<T, string> del)
        {
            sbReused.Length = 0;
            int count = 0;
            if (items == null) return string.Empty;

            for (int n = 0; n < items.Count; n++)
            {
                if (items[n] == null)
                {
                    continue;
                }

                if (count > 0)
                {
                    sbReused.Append(delimiter);
                }

                string s = del(items[n]);

                if (!string.IsNullOrEmpty(s)) { sbReused.Append(s); count++; }
            }
            return sbReused.ToString();
        }

        /// <summary>
        /// Builds a list from the enumerable items, using parameter 'delimiter' as the delimiter and function del to format each item
        /// Does not include null list items
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="delimiter"></param>
        /// <param name="del"></param>
        /// <returns></returns>
        public static string BuildList<T>(this IEnumerable<T> items, string delimiter, Func<T, string> del)
        {
            if (items == null) return string.Empty;
            StringBuilder sb = new StringBuilder(1024);
            int n = 0;

            foreach (T item in items)
            {
                if (item == null)
                {
                    continue;
                }

                if (n > 0)
                {
                    sb.Append(delimiter);
                }

                string s = del(item);

                if (!string.IsNullOrEmpty(s)) { sb.Append(s); n++; }
            }
            return sb.ToString();
        }        
    }
}
